package com.bsj.pay.service.impl;

import com.bsj.pay.common.ChargeOrder;
import com.bsj.pay.common.Result;
import com.bsj.pay.config.GlobalConfig;
import com.bsj.pay.dao.AccountDao;
import com.bsj.pay.dao.ChargeAccountDao;
import com.bsj.pay.dao.MerchantDao;
import com.bsj.pay.dao.OrderDao;
import com.bsj.pay.model.AccountBean;
import com.bsj.pay.model.ChargeAccountBean;
import com.bsj.pay.model.GoodsBean;
import com.bsj.pay.model.MerchantBean;
import com.bsj.pay.model.OrderBean;
import com.bsj.pay.service.ChargeAccountService;
import com.bsj.pay.service.GoodsService;
import com.bsj.pay.service.OrderService;
import com.bsj.pay.service.PartnerService;
import com.bsj.pay.util.HttpUtil;
import com.bsj.pay.util.SpringUtil;
import com.bsj.pay.util.ThreadPoolUtils;
import com.bsj.pay.util.platform.PlatformInterface;
import com.bsj.pay.util.security.MD5;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    private static final Logger logger = LogManager.getLogger(OrderServiceImpl.class);

    public static long lastActiveTime = 0;

    public static Map<String, Long> dispatchChargeAccountMap = new HashMap<String, Long>();

    public static Map<Long, Long> queryOrderAccountMap = new HashMap<Long, Long>();

    @Autowired
    private OrderDao dao;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private ChargeAccountDao chargeAccountDao;

    @Autowired
    private ChargeAccountService chargeAccountService;

    @Autowired
    private MerchantDao merchantDao;

    @Autowired
    private PartnerService partnerService;

    @Autowired
    private GoodsService goodsService;

    @Override
    public long insert(OrderBean t) {
        // TODO Auto-generated method stub
        return dao.insert(t);
    }

    @Override
    public int deleteById(Long id) {
        // TODO Auto-generated method stub
        return dao.deleteById(id);
    }

    @Override
    public List<OrderBean> queryList(OrderBean t) {
        // TODO Auto-generated method stub
        return dao.queryList(t);
    }

    @Override
    public int update(OrderBean t) {
        // TODO Auto-generated method stub
        return dao.update(t);
    }

    @Override
    public OrderBean getById(Long Id) {
        // TODO Auto-generated method stub
        return dao.getById(Id);
    }

    @Override
    public long count(OrderBean t) {
        // TODO Auto-generated method stub
        return dao.count(t);
    }

    @Override
    public PageInfo<OrderBean> pageList(OrderBean t, int pageNum, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageNum, pageSize);
        List<OrderBean> list = dao.queryList(t);
        PageInfo<OrderBean> pageInfo = new PageInfo<OrderBean>(list);
        return pageInfo;
    }

    @Override
    public int removeById(Long id) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<OrderBean> queryAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Transactional
    public boolean payNotify(Long orderId, String realOrderNo, int type) throws Exception {
        OrderBean order = this.getById(orderId);
        if (order == null) {
            logger.info("没有找到对应对单:" + orderId);
            throw new Exception("没有找到对应对单:" + orderId);
        }
        if (type == 1 && order.getStatus() > 1 && order.getStatus() != 5) {
            logger.info("超时不回调");
            throw new Exception("超时不回调");
        }
        if (order.getStatus() == 1 && order.getNotify_status() == 1) {
            logger.info("已回调，不处理");
            throw new Exception("已回调，不处理");
        }
        if (order.getStatus() == 0 || order.getStatus() == 5) {
            // 0 未支付   5支付确认中
            OrderBean updateOrder = new OrderBean();
            updateOrder.setId(order.getId());
            updateOrder.setStatus(1);
            if (!StringUtils.isEmpty(realOrderNo) && !realOrderNo.equals(order.getOfficial_order_no())) {
                updateOrder.setOfficial_order_no(realOrderNo);
            }
            updateOrder.setPay_time(System.currentTimeMillis() / 1000);
            this.dao.update(updateOrder);
            order.setStatus(1);
        }
        OrderBean updateOrder = new OrderBean();
        updateOrder.setId(order.getId());
        updateOrder.setNotify_times(order.getNotify_times() + 1);
        this.dao.update(updateOrder);
        MerchantBean merchant = merchantDao.getById(order.getMerchant_id());
        new Thread(new Runnable() {
            public void run() {
                try {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("order_no", order.getId().toString());
                    params.put("merchant_no", order.getMerchant_id().toString());
                    params.put("out_order_no", order.getMerchant_order_no());
                    params.put("amount", order.getAmount().toString());
                    params.put("pay_type", order.getPay_type());
                    params.put("status", order.getStatus().toString());
                    String md5 = MD5.getMD5("" + order.getId() + order.getMerchant_id() + order.getMerchant_order_no()
                            + order.getAmount() + order.getPay_type() + order.getStatus() + merchant.getApi_key());
                    params.put("sign", md5);
                    String body = HttpUtil.post(order.getNotify_url(), params);
                    if (StringUtils.isEmpty(body)) {
                        logger.info("回调给商戶,网络异常,订单号:{}" + order.getId());
                        return;
                    }
                    body = body.replaceAll("\r|\n", "").trim();
                    if ("SUCCESS".equals(body) || "success".equals(body)) {
                        OrderBean localOrder = new OrderBean();
                        localOrder.setId(order.getId());
                        localOrder.setNotify_status(1);
                        localOrder.setNotify_time(System.currentTimeMillis() / 1000);
                        dao.update(localOrder);
                        logger.info("######################回调给商户成功,订单号:{}######################", order.getId());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.info("回调给商戶,出现异常,订单号:{}", order.getId());
                }
            }
        }).start();
        return true;
    }


    @Override
    public double sumMoney(OrderBean order) {
        // TODO Auto-generated method stub
        return dao.sumMoney(order);
    }

    @Override
    public PageInfo<OrderBean> pageDayFinanceList(OrderBean t, int pageNum, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageNum, pageSize);
        List<OrderBean> list = dao.queryDayFinanceList(t);
        PageInfo<OrderBean> pageInfo = new PageInfo<OrderBean>(list);
        return pageInfo;
    }


    @Override
    public List<OrderBean> queryDayList(OrderBean order) {
        // TODO Auto-generated method stub
        return dao.queryDayList(order);
    }

    @Override
    public void MerchantNotifyTask() throws Exception {
        // TODO Auto-generated method stub
        OrderBean queryModel = new OrderBean();
        queryModel.setStatus(1);
        queryModel.setNotify_status(0);
        queryModel.setNotify_times(3);
        List<OrderBean> notNotifyList = this.dao.queryList(queryModel);
        for (OrderBean order : notNotifyList) {
            this.payNotify(order.getId(), null, 1);
        }
    }

    @Override
    public synchronized Result pay(HttpServletRequest request, String out_order_no, String pay_type, String amount, Long merchantId,
                                   String notify_url, String operator) throws Exception {
        // TODO Auto-generated method stub
        lastActiveTime = System.currentTimeMillis() / 1000;
        Result result = new Result();
        OrderBean queryModel = new OrderBean();
        queryModel.setMerchant_order_no(out_order_no);
        if (this.dao.count(queryModel) > 0L) {
            result.setCode(204);
            result.setMsg("out_order_no已存在");
            result.setData("");
            return result;
        }
        // String operator = "wsgw";
        if (GlobalConfig.operator == 2) operator = "cloud";
        AccountBean account = accountDao.getOneCanDispatchOrderByAuthType(operator, GlobalConfig.order_account_type);
        if (account == null) {
            result.setCode(0);
            result.setMsg("暂无购买账号，请稍后再试!");
            result.setData("");
            return result;
        }
        AccountBean updateModel = new AccountBean();
        updateModel.setId(account.getId());
        updateModel.setBuy_last_time(System.currentTimeMillis() / 1000);
        accountDao.update(updateModel);

        int order_amout = Integer.parseInt(amount);
        if ("weidian".equalsIgnoreCase(operator)) {
            GoodsBean goods = goodsService.getOneCanDispatchGoods(order_amout, pay_type, GlobalConfig.query_time_out);

            if (goods == null) {
                result.setCode(0);
                result.setMsg("库存不足，请稍后再试!");
                result.setData("");
                return result;
            }

            OrderBean order = new OrderBean();
            order.setStatus(-1);
            order.setMerchant_id(merchantId);
            order.setPartner_id(goods.getPartner_id());
            order.setAmount(order_amout);
            order.setCreate_time(System.currentTimeMillis() / 1000);
            order.setMerchant_order_no(out_order_no);
            order.setPay_type(pay_type);
            order.setAccount_id(account.getId());
            order.setAccount(account.getAccount());
            // order.setGoods_type(goods.getType());
            order.setNotify_url(notify_url);
            order.setGoods_id(goods.getId());
            // order.setCharge_account_id(chargeAccount.getId());
            order.setOperator(operator);
            order.setCharge_account(goods.getPhone());
            this.dao.insert(order);
            //实时异步下单
            new Thread(new Runnable() {
                public void run() {
                    logger.info("开启下单,账号ID:{},{}", order.getAccount_id(), goods.getPhone(), account.getId());
                    submitOrder(order, goods, account);
                }
            }).start();
            Map<String, String> map = new HashMap<String, String>();
            map.put("merchant_no", String.valueOf(order.getMerchant_id()));
            map.put("order_no", String.valueOf(order.getId()));
            map.put("out_order_no", order.getMerchant_order_no());
            map.put("amount", String.valueOf(order.getAmount()));
            map.put("pay_type", order.getPay_type());
            map.put("pay_url", HttpUtil.getServerPath(request) + "api/payPage.html?id=" + order.getId());
            result.setCode(1);
            result.setMsg("请求成功");
            result.setData(map);
            return result;
        } else {
            ChargeAccountBean chargeAccount;
            chargeAccount = chargeAccountDao.getOneCanDispatchOrder(order_amout, GlobalConfig.query_time_out);
            if (chargeAccount == null) {
                result.setCode(0);
                result.setMsg("库存不足，请稍后再试!");
                result.setData("");
                return result;
            }
		/*if(dispatchChargeAccountMap.containsKey(chargeAccount.getAccount())&&dispatchChargeAccountMap.get(chargeAccount.getAccount())>System.currentTimeMillis()/1000-2)
		{

			result.setCode(0);
			result.setMsg("系统处理繁忙，请稍后重试!");
			result.setData("");
			return result;
		}
		else
		{
			dispatchChargeAccountMap.put(chargeAccount.getAccount(), System.currentTimeMillis()/1000);
		}*/

            ChargeAccountBean updateCharge = new ChargeAccountBean();
            updateCharge.setId(chargeAccount.getId());
            updateCharge.setUpdate_time(System.currentTimeMillis() / 1000);
            chargeAccountService.update(updateCharge);

            OrderBean order = new OrderBean();
            order.setStatus(-1);
            order.setMerchant_id(merchantId);
            order.setPartner_id(chargeAccount.getPartner_id());
            order.setAmount(order_amout);
            order.setCreate_time(System.currentTimeMillis() / 1000);
            order.setMerchant_order_no(out_order_no);
            order.setPay_type(pay_type);
            order.setAccount_id(account.getId());
            order.setAccount(account.getAccount());
            order.setGoods_type(chargeAccount.getType());
            order.setNotify_url(notify_url);
            order.setCharge_account_id(chargeAccount.getId());
            order.setOperator(operator);
            order.setCharge_account(chargeAccount.getAccount());
            this.dao.insert(order);
            //实时异步下单
            new Thread(new Runnable() {
                public void run() {
                    logger.info("开启下单,账号ID:{},{}", order.getAccount_id(), chargeAccount.getAccount(), account.getId());
                    submitOrder(order, chargeAccount, account);
                }
            }).start();
            Map<String, String> map = new HashMap<String, String>();
            map.put("merchant_no", String.valueOf(order.getMerchant_id()));
            map.put("order_no", String.valueOf(order.getId()));
            map.put("out_order_no", order.getMerchant_order_no());
            map.put("amount", String.valueOf(order.getAmount()));
            map.put("pay_type", order.getPay_type());
            map.put("pay_url", HttpUtil.getServerPath(request) + "api/payPage.html?id=" + order.getId());
            result.setCode(1);
            result.setMsg("请求成功");
            result.setData(map);
            return result;
        }
    }

    private void submitOrder(OrderBean order, GoodsBean goods, AccountBean account) {
        Map<String, String> resultMap = null;
        OrderBean updateOrder = new OrderBean();
        updateOrder.setId(order.getId());
        try {
            PlatformInterface inf = (PlatformInterface) SpringUtil.getBean(order.getOperator() + "Util");
            resultMap = inf.createOrder(null, goods, account, order);

            updateOrder.setStatus(0);
            updateOrder.setGoods_id(Long.parseLong(resultMap.get("goodsId")));
            updateOrder.setOfficial_order_no(resultMap.get("orderNo"));
            updateOrder.setPay_url(resultMap.get("payUrl"));
            updateOrder.setLogin_cookie(resultMap.get("cookies"));
            if (resultMap.containsKey("notifyToken")) {
                updateOrder.setNotify_token(resultMap.get("notifyToken"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            updateOrder.setStatus(4);
            updateOrder.setRemarks(e.getMessage());
            if (null != e.getMessage()) {
                AccountBean updateModel = new AccountBean();
                // TODO: 2022/12/17 需要判断账号异常，cookie是否失效，失效后，更新login_status为2
                boolean needUpdate = false;
                if (e.getMessage().contains("网络链接失败")) {
                    accountDao.addProxyFailTimes(account.getId());
                } else if (e.getMessage().contains("请重新登录")) {
                    updateModel.setLogin_status(2);
                    updateModel.setLogin_cookie("");
                    needUpdate = true;
                }
                if (e.getMessage().contains("系统处理繁忙") || e.getMessage().contains("当日支付次数超限")) {
                    updateModel.setLogin_status(4);
                    needUpdate = true;
                }
                if (needUpdate) {
                    updateModel.setId(account.getId());
                    updateModel.setUpdate_time(System.currentTimeMillis() / 1000);
                    updateModel.setRemarks(e.getMessage());
                    accountDao.update(updateModel);
                }
            }
        } finally {
            updateOrder.setUpdate_time(System.currentTimeMillis() / 1000);
            dao.update(updateOrder);
        }
    }

    private void submitOrder(OrderBean order, ChargeAccountBean chargeAccount, AccountBean account) {
        Map<String, String> resultMap = null;
        OrderBean updateOrder = new OrderBean();
        updateOrder.setId(order.getId());
        ChargeAccountBean updateCharge = new ChargeAccountBean();
        updateCharge.setId(order.getCharge_account_id());
        try {
            PlatformInterface inf = (PlatformInterface) SpringUtil.getBean(order.getOperator() + "Util");
            resultMap = inf.createOrder(chargeAccount, null, account, order);

            updateOrder.setStatus(0);
            updateOrder.setGoods_id(Long.parseLong(resultMap.get("goodsId")));
            updateOrder.setOfficial_order_no(resultMap.get("orderNo"));
            updateOrder.setPay_url(resultMap.get("payUrl"));
            updateOrder.setLogin_cookie(resultMap.get("cookies"));

            chargeAccountDao.addChargeCount(chargeAccount.getId());
        } catch (Exception e) {
            e.printStackTrace();
            updateCharge.setStatus(chargeAccount.getStatus());
            updateCharge.setCharge_status(chargeAccount.getCharge_status());
            updateCharge.setRemarks(e.getMessage());
            updateOrder.setStatus(4);
            updateOrder.setRemarks(e.getMessage());
            if (null != e.getMessage()) {
                AccountBean updateModel = new AccountBean();
                boolean needUpdate = false;
                if (e.getMessage().contains("网络链接失败")) {
                    accountDao.addProxyFailTimes(account.getId());
                } else if (e.getMessage().contains("请重新登录")) {
                    updateModel.setLogin_status(2);
                    updateModel.setLogin_cookie("");
                    needUpdate = true;
                }
                if (e.getMessage().contains("系统处理繁忙") || e.getMessage().contains("当日支付次数超限")) {
                    updateModel.setLogin_status(4);
                    needUpdate = true;
                }
                if (needUpdate) {
                    updateModel.setId(account.getId());
                    updateModel.setUpdate_time(System.currentTimeMillis() / 1000);
                    updateModel.setRemarks(e.getMessage());
                    accountDao.update(updateModel);
                }
            }
        } finally {
            updateCharge.setUpdate_time(System.currentTimeMillis() / 1000);
            chargeAccountDao.update(updateCharge);
            updateOrder.setUpdate_time(System.currentTimeMillis() / 1000);
            dao.update(updateOrder);
        }
    }

    @Override
    public void QueryOrderTask() throws Exception {
        // TODO Auto-generated method stub
        List<OrderBean> needCheckOrderList = this.dao.getNeedCheckOrderList(GlobalConfig.query_time_out);
        if (needCheckOrderList == null || needCheckOrderList.size() == 0) return;
        lastActiveTime = System.currentTimeMillis() / 1000;
        for (OrderBean order : needCheckOrderList) {
            ThreadPoolUtils.getQueryOrderStatePool().execute(() -> {
                logger.info("开启在线查单子线程,订单ID:{}", order.getId());
                this.queryOrderStatus(order, 1);
                logger.info("在线查单子线程已结束！");
            });
        }
        do {
            Thread.sleep(1000);
        } while (ThreadPoolUtils.getQueryOrderStatePool().getActiveCount() != 0);
    }

    @Override
    public void QueryAccountOrderTask() throws Exception {
        // TODO Auto-generated method stub
        List<OrderBean> needCheckOrderAccountList = this.dao.getNeedCheckOrderAccountIdList(GlobalConfig.query_time_out);
        if (needCheckOrderAccountList == null || needCheckOrderAccountList.size() == 0) return;
        lastActiveTime = System.currentTimeMillis() / 1000;
        for (OrderBean order : needCheckOrderAccountList) {
            ThreadPoolUtils.getQueryAccountOrderStatePool().execute(() -> {
                logger.info("开启账号在线查单子线程,账号ID:{}", order.getAccount_id());
                this.queryAccountOrderStatus(order, 1);
                logger.info("在线账号查单子线程已结束！");
            });
        }
        do {
            Thread.sleep(1000);
        } while (ThreadPoolUtils.getQueryAccountOrderStatePool().getActiveCount() != 0);
    }

    @Override
    public void queryOrderStatus(OrderBean order, int sucStatus) {
        // TODO Auto-generated method stub
        AccountBean account = accountDao.getById(order.getAccount_id());
        try {
            PlatformInterface inf = (PlatformInterface) SpringUtil.getBean(order.getOperator() + "Util");
            String queryOrderType = inf.getQueryOrderType();
            if ("wsgw".equals(order.getOperator())) {
                if (GlobalConfig.notify_type == 1) queryOrderType = "account";
                else queryOrderType = "order";
            }
            if ("order".equals(queryOrderType))//根据单个订单查单
            {
                //如果下单账号查询超限，使用临时账号去查
                if (account.getLogin_status() == 4 && "wsgw".equals(order.getOperator())) {
                    AccountBean accountTemp = accountDao.getOneCanDispatchOrderByOperatorAndTime("wsgw");
                    if (accountTemp != null) {
                        logger.info("账号" + account.getAccount() + "失效,使用临时账号" + accountTemp.getAccount() + "查单");
                        account.setIs_temp(true);
                        account.setTemp_id(accountTemp.getId());
                        account.setLogin_cookie(accountTemp.getLogin_cookie());
                    }
                }
                Map<String, String> resultMap = inf.orderDetail(order, account, GlobalConfig.notify_type);
                String status = resultMap.get("status");
                String orderNo = resultMap.get("orderNo");
                if ("success".equals(status)) {
                    logger.info("(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)");
                    accountDao.addBuyCount(order.getAccount_id());
                    this.payNotify(order.getId(), orderNo, 1);
                    if (order.getCharge_account_id() != null) {
                        chargeAccountService.procesSuccOrder(order.getCharge_account_id());
                    }
                    partnerService.subQuota(order.getPartner_id(), order.getAmount(), orderNo);
                }
            } else if ("account".equals(queryOrderType))//根据账号查单,8秒之内不允许重复查单
            {
                if (queryOrderAccountMap.containsKey(order.getAccount_id()) && queryOrderAccountMap.get(order.getAccount_id()) > System.currentTimeMillis() / 1000 - 10) {
                    return;
                }
                queryOrderAccountMap.put(order.getAccount_id(), System.currentTimeMillis() / 1000);
                List<ChargeOrder> successList = inf.queryTransList(account, true);
                if (successList == null) return;
                for (ChargeOrder o : successList) {
                    boolean payFlag = false;
                    OrderBean lOrder = null;
                    if ("wsgw".equals(account.getOperator()) || "cloud".equals(account.getOperator())) {
                        lOrder = dao.selectByOfficialOrderNo(o.getOrderNo());
                        if (lOrder != null && lOrder.getStatus() == 0) payFlag = true;
                    }
                    if (payFlag) {
                        logger.info("(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)");
                        accountDao.addBuyCount(order.getAccount_id());
                        this.payNotify(lOrder.getId(), o.getOrderNo(), 1);
                        chargeAccountService.procesSuccOrder(lOrder.getCharge_account_id());
                        partnerService.subQuota(lOrder.getPartner_id(), lOrder.getAmount(), o.getOrderNo());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("账号异常,账号:{},{}", account.getAccount(), e.getMessage());
            if (e.getMessage().contains("请重新登录")) {
                AccountBean updateModel = new AccountBean();
                updateModel.setId(order.getAccount_id());
                if (account.isIs_temp()) updateModel.setId(account.getTemp_id());
                updateModel.setLogin_status(2);
                updateModel.setLogin_cookie("");
                updateModel.setUpdate_time(System.currentTimeMillis() / 1000);
                accountDao.update(updateModel);
            } else if (e.getMessage() != null && e.getMessage().contains("网络链接失败") || e.getMessage().contains("您的操作太频繁")) {
                accountDao.addProxyFailTimes(order.getAccount_id());
            }
        }

    }

    @Override
    public void queryAccountOrderStatus(OrderBean order, int sucStatus) {
        // TODO Auto-generated method stub
        AccountBean account = accountDao.getById(order.getAccount_id());
        try {
            PlatformInterface inf = (PlatformInterface) SpringUtil.getBean(account.getOperator() + "Util");
            List<ChargeOrder> successList = inf.queryTransList(account, true);
            if (successList == null) return;
            for (ChargeOrder o : successList) {
                if (o.getCreateTime() < System.currentTimeMillis() / 1000 - 300) continue;
                boolean payFlag = false;
                OrderBean lOrder = null;
                if ("wsgw".equals(account.getOperator()) || "cloud".equals(account.getOperator())) {
                    lOrder = dao.selectByOfficialOrderNo(o.getOrderNo());
                    if (lOrder != null && lOrder.getStatus() == 0) payFlag = true;
                }
                if (payFlag) {
                    logger.info("(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)(●'◡'●)");
                    accountDao.addBuyCount(order.getAccount_id());
                    this.payNotify(lOrder.getId(), o.getOrderNo(), 1);
                    chargeAccountService.procesSuccOrder(lOrder.getCharge_account_id());
                    partnerService.subQuota(lOrder.getPartner_id(), lOrder.getAmount(), o.getOrderNo());
                }
            }
        } catch (Exception e) {
            logger.info("账号异常,账号:{},{}", account.getAccount(), e.getMessage());
            if (e.getMessage().contains("网络链接失败") || e.getMessage().contains("您的操作太频繁")) {
                accountDao.addProxyFailTimes(order.getAccount_id());
            } else if (e.getMessage().contains("请重新登录")) {
                AccountBean updateModel = new AccountBean();
                updateModel.setId(order.getAccount_id());
                updateModel.setLogin_status(2);
                updateModel.setLogin_cookie("");
                updateModel.setUpdate_time(System.currentTimeMillis() / 1000);
                accountDao.update(updateModel);
            }
        }
    }


    @Override
    public int insertTemp(OrderBean order) {
        // TODO Auto-generated method stub
        return dao.insertTemp(order);
    }

    @Override
    public int removeByTime(long startTime, long endTime) {
        // TODO Auto-generated method stub
        return dao.removeByTime(startTime, endTime);
    }

    @Override
    public PageInfo<OrderBean> notGetCdkPageList(OrderBean t, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderBean> list = this.dao.notGetCdkList(t);
        PageInfo<OrderBean> pageInfo = new PageInfo<OrderBean>(list);
        return pageInfo;
    }

    @Override
    public int updateGetCdkSmsBySendTime(String phone, String verCode) {
        // TODO Auto-generated method stub
        return dao.updateGetCdkSmsBySendTime(phone, verCode);
    }

    @Override
    public int reGetAll() {
        // TODO Auto-generated method stub
        return dao.reGetAll();
    }

    @Override
    public OrderBean selectLastByChargeAccount(String charge_account) {
        // TODO Auto-generated method stub
        return dao.selectLastByChargeAccount(charge_account);
    }

    @Override
    public OrderBean getLastByChargeAccountId(Long chargeAccountId) {
        // TODO Auto-generated method stub
        return dao.getLastByChargeAccountId(chargeAccountId);
    }
}
